extends layout

block headContent
	title Address #{address}

block content
	if (result && result.validateaddress)
		if (!result.validateaddress.isvalid)
			h1(class="h3 text-danger") Invalid Address
				br
				small(class="monospace") #{address}
		else
			h1(class="h3") Address
				br
				small(class="monospace") #{address}
			
			hr

			if (payoutAddressForMiner)
				div(class="alert alert-primary", style="padding-bottom: 0;")
					div(class="float-left", style="width: 50px; height: 50px; font-size: 18px;")
						i(class="fas fa-certificate fa-2x", style="margin-top: 10px;")

					h4(class="alert-heading h6 font-weight-bold") #{coinConfig.name} Fun

					p
						span This looks like a miner payout address for  
						span(class="font-weight-bold") #{payoutAddressForMiner.name}
						if (coinConfig.miningPoolsConfigUrls.length > 1)
							span  (see the configurations: 
							each item, index in coinConfig.miningPoolsConfigUrls
								if (index > 0)
									span , 
								a(href=item) config ##{index + 1}
							span )
						else
							span  (see the configuration
							a(href=coinConfig.miningPoolsConfigUrls[0]) here
							span )
			else if (global.specialAddresses[address] && global.specialAddresses[address].type == "donation")
				div(class="alert alert-primary", style="padding-bottom: 0;")
					div(class="float-left", style="width: 50px; height: 50px; font-size: 18px;")
						i(class="fas fa-certificate fa-2x", style="margin-top: 10px;")

					h4(class="alert-heading h6 font-weight-bold") #{coinConfig.name} Fun

					p
						span This is the #{coinConfig.ticker} donation address to support development of this tool. All support is appreciated!

			else if (global.specialAddresses[address] && global.specialAddresses[address].type == "fun")
				div(class="alert alert-primary", style="padding-bottom: 0;")
					div(class="float-left", style="width: 50px; height: 50px; font-size: 18px;")
						i(class="fas fa-certificate fa-2x", style="margin-top: 10px;")

					h4(class="alert-heading h6 font-weight-bold") #{coinConfig.name} Fun

					// special address info
					- var saInfo = global.specialAddresses[address].addressInfo;
					if (saInfo.alertBodyHtml)
						p
							span !{saInfo.alertBodyHtml}

							if (saInfo.referenceUrl && saInfo.referenceUrl.trim().length > 0 && saInfo.alertBodyHtml.indexOf(saInfo.referenceUrl) == -1)
								span  
									a(href=saInfo.referenceUrl) Read more

					else
						p
							span #{saInfo.summary}

							if (saInfo.referenceUrl && saInfo.referenceUrl.trim().length > 0)
								span  
									a(href=saInfo.referenceUrl) Read more


			if (false)
				pre
					code #{JSON.stringify(addressObj, null, 4)}
			
			ul(class='nav nav-tabs mb-3')
				li(class="nav-item")
					a(data-toggle="tab", href="#tab-details", class="nav-link active", role="tab") Details
				li(class="nav-item")
					a(data-toggle="tab", href="#tab-json", class="nav-link", role="tab") JSON


			div(class="tab-content")
				div(id="tab-details", class="tab-pane active", role="tabpanel")

					if (config.electrumXServers && config.electrumXServers.length > 0)
						if (session.hideElectrumTrustWarnings != "true")
							div(class="alert alert-primary alert-dismissible clearfix", role="alert")
								h4(class="alert-heading h6 font-weight-bold") Note
								p Since this explorer is database-free, it doesn't natively support address balances and transaction histories. In order to provide this functionality, address balances and transaction history can be requested from a configurable set of ElectrumX servers. If multiple ElectrumX servers are configured, the results are cross-referenced and conflicts noted. For the transaction history displayed below, only the transaction identifiers from ElectrumX are used; the transaction details are requested via RPC from this app's primary node, as usual.

								span(class="font-weight-bold") Configured ElectrumX Servers
								ul
									each server in config.electrumXServers
										li
											span #{server.host}
											span  : #{server.port}

								a(href="/changeSetting?name=hideElectrumTrustWarnings&value=true", class="close", aria-label="Close", style="text-decoration: none;")
									span(aria-hidden="true") &times;

					div(class="card mb-3")
						div(class="card-header")
							span(class="h6") Summary
						div(class="card-body")
							div(class="row")
								div(class="col-md-6")

									if (addressObj.hash)
										div(class="row")
											div(class="summary-split-table-label") Hash 160
											div(class="summary-split-table-content monospace") #{addressObj.hash.toString("hex")}

									if (result.validateaddress.scriptPubKey)
										div(class="row")
											div(class="summary-split-table-label") Script Public Key
											div(class="summary-split-table-content monospace") #{result.validateaddress.scriptPubKey}

									if (addressObj.hasOwnProperty("version"))
										div(class="row")
											div(class="summary-split-table-label") Version
											div(class="summary-split-table-content monospace") #{addressObj.version}

									if (result.validateaddress.hasOwnProperty("witness_version"))
										div(class="row")
											div(class="summary-split-table-label") Witness Version
											div(class="summary-split-table-content monospace") #{result.validateaddress.witness_version}

									if (result.validateaddress.witness_program)
										div(class="row")
											div(class="summary-split-table-label") Witness Program
											div(class="summary-split-table-content monospace") #{result.validateaddress.witness_program}

									if (firstSeenTransaction)
										div(class="row")
											div(class="summary-split-table-label") First Seen
											div(class="summary-split-table-content monospace")
												if (getblockchaininfo)
													span Block ##{(getblockchaininfo.blocks - firstSeenTransaction.confirmations).toLocaleString()}
												else
													a(href=("/block/" + firstSeenTransaction.blockhash)) #{firstSeenTransaction.blockhash}

												br

												span #{moment.utc(new Date(firstSeenTransaction.time * 1000)).format("Y-MM-DD HH:mm:ss")} utc
												- var timeAgoTime = firstSeenTransaction.time;
												include includes/time-ago.pug
												
												if (false)
													pre
														code #{JSON.stringify(firstSeenTransaction)}

									if (balance)
										if (balance.conflictedResults)
											div(class="row")
												div(class="summary-split-table-label") Balance
												div(class="summary-split-table-content monospace")
													span(class="text-danger") Conflicted ElectrumX Results

													each item in balance.conflictedResults
														- var currencyValue = item.confirmed / coinConfig.baseCurrencyUnit.multiplier;
														include includes/value-display.pug
														

										else
											div(class="row")
												div(class="summary-split-table-label") Balance
												div(class="summary-split-table-content monospace")
													- var currencyValue = balance.result.confirmed / coinConfig.baseCurrencyUnit.multiplier;
													include includes/value-display.pug

											if (balance.unconfirmed)
												div(class="row")
													div(class="summary-split-table-label") Unconfirmed
													div(class="summary-split-table-content monospace")
														- var currencyValue = balance.unconfirmed / coinConfig.baseCurrencyUnit.multiplier;
														include includes/value-display.pug

									if (electrumHistory)
										div(class="row")
											div(class="summary-split-table-label") Transactions
											div(class="summary-split-table-content monospace") #{electrumHistory.result.length.toLocaleString()}

									div(class="row")
										div(class="summary-split-table-label") QR Code
										div(class="summary-split-table-content monospace")
											img(src=addressQrCodeUrl, alt=address, style="border: solid 1px #ccc;")

								div(class="col-md-6")
									if (electrumScripthash)
										div(class="row")
											div(class="summary-split-table-label") Scripthash
											div(class="summary-split-table-content monospace") #{electrumScripthash}

									- var x = result.validateaddress;
									- var flagNames = ["Is Valid?", "Is Script?", "Is Witness?", "Is Mine?", "Is Watch-Only?"];
									- var flags = [x.isvalid, x.isscript, x.iswitness, x.ismine, x.iswatchonly];

									each flagName, index in flagNames
										div(class="row")
											div(class="summary-split-table-label") #{flagName}
											div(class="summary-split-table-content monospace")
												if (flags[index])
													i(class="fas fa-check text-success")
												else
													i(class="fas fa-times text-danger")

					if (false)
						div(class="card mb-3")
							div(class="card-header")
								span(class="h6") Flags
							div(class="card-body")
								table(class="table table-responsive-sm text-center")
									thead
										tr
											th Is Valid?
											th Is Script?
											th Is Witness?
											th Is Mine?
											th Is Watch-Only?
									tbody
										tr
											- var x = result.validateaddress;
											- var flags = [x.isvalid, x.isscript, x.iswitness, x.ismine, x.iswatchonly];

											each flag in flags
												td
													if (flag)
														i(class="fas fa-check text-success")
													else
														i(class="fas fa-times text-danger")

					div(class="card")
						div(class="card-header clearfix")
							div(class="float-left")
								span(class="h6")
									if (txids)
										if (txids.length == 1)
											span 1 Transaction
										else
											span #{txids.length.toLocaleString()} Transactions
									else
										span Transactions

							if (!crawlerBot && txids && txids.length > 1)
								div(class="float-right")
									a(href="#", class="pull-right dropdown-toggle", data-toggle="dropdown", aria-haspopup="true", aria-expanded="false")
										if (sort == "desc")
											span Newest First
										else
											span Oldest First

									div(class="dropdown-menu dropdown-menu-right")
										a(href=("/address/" + address), class="dropdown-item")
											if (sort == "desc")
												i(class="fa fa-check") 
											span  Newest First
										a(href=("/address/" + address + "?sort=asc"), class="dropdown-item")
											if (sort != "desc")
												i(class="fa fa-check") 
											span  Oldest First

						div(class="card-body")
							if (conflictedTxidResults)
								div(class="alert alert-warning", style="padding-bottom: 0;")
									div(class="float-left", style="width: 55px; height: 50px; font-size: 18px;")
										i(class="fas fa-exclamation-triangle fa-2x", style="margin-top: 10px;")
									h4(class="alert-heading h6 font-weight-bold") Trust Warning
									p
										span The transaction history for this address was requested from mulitple ElectrumX servers and the results did not match. The results below were obtained only from 
										span(class="font-weight-bold") #{electrumHistory.server}

							
							if (advancedFunctionality)
								if (electrumHistoryError && electrumHistoryError.error && electrumHistoryError.error.code && electrumHistoryError.error.code == -32600)
									span Failed to retrieve transaction history from ElectrumX. See 
									a(href="https://https://gitee.com/zhanghoufu/pkc-rpc-explorer/issues/67") Issue #67
									span  for more information.

								else if (transactions.length == 0)
									span No transactions found

								each tx, txIndex in transactions
									//pre
									//	code #{JSON.stringify(tx, null, 4)}
									div(class="xcard mb-3")
										div(class="card-header monospace clearfix")
											div(class="float-left", style="margin-right: 0px;")
												if (sort == "desc")
													span ##{(txids.length - offset - txIndex).toLocaleString()}
												else
													span ##{(offset + txIndex + 1).toLocaleString()}
												span  &ndash;

											div(class="row")
												div(class="col-md-6")
													if (tx && tx.txid)
														a(href=("/tx/" + tx.txid)) #{tx.txid}

														if (global.specialTransactions && global.specialTransactions[tx.txid])
															span  
															a(data-toggle="tooltip", title=(coinConfig.name + " Fun! See transaction for details"))
																i(class="fas fa-certificate text-primary")

												div(class="col-md-6")
													div(class="text-md-right")
														if (tx.time)
															span #{moment.utc(new Date(tx["time"] * 1000)).format("Y-MM-DD HH:mm:ss")} utc
															- var timeAgoTime = tx.time;
															include includes/time-ago.pug

														else
															span(class="text-danger") Unconfirmed

												div(class="col")
													if (addrGainsByTx[tx.txid])
														- var currencyValue = addrGainsByTx[tx.txid];
														span(class="text-success") +
															include includes/value-display.pug

														if (addrLossesByTx[tx.txid])
															span  / 

													if (addrLossesByTx[tx.txid])
														- var currencyValue = addrLossesByTx[tx.txid];
														span(class="text-danger") -
															include includes/value-display.pug
													
										div(class="card-body")
											if (true)
												- var txInputs = txInputsByTransaction[tx.txid];
												- var blockHeight = blockHeightsByTxid[tx.txid];

												include includes/transaction-io-details.pug

							else
								p Since this explorer is database-free, it doesn't natively support address transaction history. However, you can configure it to communicate with one or more ElectrumX servers to build and display this data. In doing so, you should be aware that you'll be trusting those ElectrumX servers. If you configure multiple servers the results obtained from each will be cross-referenced against the others. Communicating with ElectrumX servers will also impact your privacy since the servers will know what addresses you're interested in. If these tradeoffs are acceptable, you can see a list of public ElectrumX servers here: 
								a(href="https://uasf.saltylemon.org/electrum") https://uasf.saltylemon.org/electrum
							
							if (false)
								pre
									code #{JSON.stringify(transactions, null, 4)}

							if (!crawlerBot && txids && txids.length > limit)
								- var pageNumber = offset / limit + 1;
								- var pageCount = Math.floor(txids.length / limit);
								- if (pageCount * limit < txids.length) {
									- pageCount++;
								- }
								- var paginationUrlFunction = function(x) {
									- return paginationBaseUrl + "&limit=" + limit + "&offset=" + ((x - 1) * limit);
								- }

								hr

								include includes/pagination.pug

							
				

				div(id="tab-json", class="tab-pane", role="tabpanel")
					div(class="highlight")
						h4 Node.ValidateAddress
						pre
							code(class="language-json", data-lang="json") #{JSON.stringify(result.validateaddress, null, 4)}

						if (config.electrumXServers && config.electrumXServers.length > 0)
							h4 Electrum.Balance
							pre
								code(class="language-json", data-lang="json") #{JSON.stringify(electrumBalance, null, 4)}

							h4 Electrum.History
							pre
								code(class="language-json", data-lang="json") #{JSON.stringify(electrumHistory, null, 4)}

		
